﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace LdCms.Web.Controllers.MVC.Map
{
    using LdCms.EF.DbModels;
    using LdCms.IBLL.Map;
    using LdCms.Common.Extension;
    using LdCms.Common.Utility;
    using LdCms.Common.Web;
    using LdCms.Common.Json;
    using LdCms.Common.Enum;
    using LdCms.Common.Security;
    using LdCms.Web.Services;
    using LdCms.Web.Models;
    using LdCms.IBLL.Sys;

    public class MapAmapPlatformController : BaseController
    {
        private readonly IBaseManager BaseManager;
        private readonly IAmapPlatformService AmapPlatformService;
        public MapAmapPlatformController(IBaseManager BaseManager, IAmapPlatformService AmapPlatformService) : base(BaseManager)
        {
            this.BaseManager = BaseManager;
            this.AmapPlatformService = AmapPlatformService;
        }

        public override IActionResult Index()
        {
            return View();
        }


        public IActionResult List()
        {
            try
            {
                string funcId = PermissionEnum.CodeFormat((int)PermissionEnum.扩展管理.友情链接.列表);
                if (!IsPermission(funcId)) { return ToPermission(funcId); }
                var lists = AmapPlatformService.GetAmapPlatformAll(SystemID, CompanyID);
                ViewData["Count"] = AmapPlatformService.CountAmapPlatform(SystemID, CompanyID);
                return View(lists);
            }
            catch (Exception ex)
            {
                return ToError(ex.Message);
            }
        }
        public IActionResult Add(string platformId)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(platformId))
                    return View(new Ld_Map_AmapPlatform());
                var entity = AmapPlatformService.GetAmapPlatform(SystemID, CompanyID, platformId);
                if (entity == null)
                    return View(new Ld_Map_AmapPlatform());
                return View(entity);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


        [HttpPost]
        public JsonResult Save(string platformId)
        {
            try
            {
                string fName = GetFormValue("fName");
                string fKey = GetFormValue("fKey");
                string fPrivateKey = GetFormValue("fPrivateKey");
                string fIsDefault = GetFormValue("fIsDefault");
                string fRemark = GetFormValue("fRemark");
                string fState = GetFormValue("fState");

                var primaryKey = PrimaryKeyHelper.PrimaryKeyType.MapAmapPlatform;
                var primaryKeyLen = PrimaryKeyHelper.PrimaryKeyLen.V1;
                string createPlatformId = PrimaryKeyHelper.MakePrimaryKey(primaryKey, primaryKeyLen);
                string fPlatformId = string.IsNullOrEmpty(platformId) ? createPlatformId : platformId;

                Ld_Map_AmapPlatform entity = new Ld_Map_AmapPlatform();
                entity.SystemID = SystemID;
                entity.CompanyID = CompanyID;
                entity.PlatformID = fPlatformId;
                entity.Name = fName;
                entity.Key = fKey;
                entity.PrivateKey = fPrivateKey;
                entity.IsDefault = fIsDefault.ToBool();
                entity.Remark = fRemark;
                entity.State = fState.ToBool();

                bool result = false;
                if (string.IsNullOrEmpty(platformId))
                    result = AmapPlatformService.SaveAmapPlatform(entity);
                else
                    result = AmapPlatformService.UpdateAmapPlatform(entity);
                if (result)
                    return Success("ok");
                else
                    return Error("fail");
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }
        [HttpPost]
        public JsonResult UpdateDefault(string platformId, bool state)
        {
            try
            {
                string funcId = PermissionEnum.CodeFormat((int)PermissionEnum.栏目管理.栏目分类.审核);
                if (!IsPermission(funcId)) { return Error("您没有操作权限，请联系系统管理员！"); }
                var result = AmapPlatformService.UpdateAmapPlatformDefault(SystemID, CompanyID, platformId, state);
                if (result)
                    return Success("ok");
                else
                    return Error("fail");
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }
        [HttpPost]
        public JsonResult Delete(string platformId)
        {
            try
            {
                string funcId = PermissionEnum.CodeFormat((int)PermissionEnum.栏目管理.栏目分类.删除);
                if (!IsPermission(funcId)) { return Error("您没有操作权限，请联系系统管理员！"); }

                var result = AmapPlatformService.DeleteAmapPlatform(SystemID, CompanyID, platformId);
                if (result)
                    return Success("ok");
                else
                    return Error("fail");
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }


    }
}